const paginationData = {
  total: 10,           // 总记录数
  current_page: 1,      // 当前页
  per_page: 10,         // 每页显示多少条
  last_page: 2,        // 最后一页
};

function goToPage(pageNumber) {
  if (pageNumber < 1 || pageNumber > paginationData.last_page) return; // 防止跳转到非法页码
  paginationData.current_page = pageNumber;
  // 在这里根据页码重新请求数据
  fetchData(pageNumber);
  // 更新分页
  generatePagination(paginationData);
}

function generatePagination(paginationData) {
    const $paginationElement = $('#pagination');
    // 清空当前分页内容
    $paginationElement.empty();

    // 上一页
    let $prevPage = $('<li class="page-item"></li>');
    $prevPage.append(`<a class="page-link" href="#" aria-label="Previous" onclick="goToPage(${paginationData.current_page - 1})">
                        <span aria-hidden="true">&laquo;</span>
                      </a>`);
    if (paginationData.current_page === 1) {
      $prevPage.addClass('disabled');
    }
    $paginationElement.append($prevPage);


    // 计算页码范围
    let startPage, endPage;
    if (paginationData.last_page <= 5) {
        startPage = 1;
        endPage = paginationData.last_page;
    } else {
        if (paginationData.current_page <= 3) {
            startPage = 1;
            endPage = 5;
        } else if (paginationData.current_page + 2 >= paginationData.last_page) {
            startPage = paginationData.last_page - 4;
            endPage = paginationData.last_page;
        } else {
            startPage = paginationData.current_page - 2;
            endPage = paginationData.current_page + 2;
        }
    }

    // 生成页码
    for (let i = startPage; i <= endPage; i++) {
      let $pageItem = $('<li class="page-item"></li>');
      if (i === paginationData.current_page) {
        $pageItem.addClass('active');
      }
      $pageItem.append(`<a class="page-link" href="#" onclick="goToPage(${i})">${i}</a>`);
      $paginationElement.append($pageItem);
    }

    // 下一页
    let $nextPage = $('<li class="page-item"></li>');
    $nextPage.append(`<a class="page-link" href="#" aria-label="Next" onclick="goToPage(${paginationData.current_page + 1})">
                        <span aria-hidden="true">&raquo;</span>
                      </a>`);
    if (paginationData.current_page === paginationData.last_page) {
      $nextPage.addClass('disabled');
    }
    $paginationElement.append($nextPage);


}



function fetchData(page) {
    // 发起 AJAX 请求
    $.ajax({
        url: '/articles/list',  // 目标URL（根据实际情况修改）
        type: 'GET',  // 请求方法
        data: {pageNo:page, pageSize:6},  // 请求数据
        success: function(response){
            // 处理成功的响应
            console.log(response.data);
            let content = '';
            // 循环遍历返回的数据并构建HTML结构
            response.data.records.forEach(function(item) {
                let html = `<div class="card-active card cursor-pointer mb-4">
                                <div class="card-body">
                                    <a class="" href="article/${item.id}"><h2 class="card-title">${item.title}</h2></a>
                                    <p class="card-text">${item.content}</p>
                                </div>
                                <div class="card-footer text-muted">
                                    发布于 ${item.publishedDate}
                                </div>
                            </div>`;
                content += html;
            });
            $("#articles").html(content);

            // 获取分页设置
            let current = response.data.current;
            let pages = response.data.pages;

            // 更新接口返回的数据
            paginationData.total = response.data.total;
            paginationData.current_page = response.data.current;
            paginationData.per_page = response.data.size;
            paginationData.last_page = response.data.pages;

            // 重新生成分页
            generatePagination(paginationData);
        },
        error: function(xhr, status, error){
            // 处理请求失败的情况
            alert('登录失败');
            console.error(error);
        }
    });
}

$(document).ready(function(){
    goToPage(1);
});